package com.xudow.app.ui.adapter.user;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.activeshare.edu.ucenter.models.coupons.CouponsWithOther;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.util.ImageUtils;

import java.util.List;

/**
 * 优惠券Adapter
 *
 * @author lukecheng
 */
public class CouponAdapter extends ArrayAdapter<CouponsWithOther> {

    public CouponAdapter(Context context, int resource, List<CouponsWithOther> objects) {
        super(context, resource, objects);
    }
    public void setData(){

    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

          if (null == convertView) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_coupon_list, null);

            holder = new ViewHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.image);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        CouponsWithOther coupon = getItem(position);

        String imagePath = String.format(Config.IMAGE_LOAD_URL_PARAMS, coupon.getCouponType().getImgPath());
       holder.image.setTag(imagePath);
        if (null!=holder.image.getTag()&&holder.image.getTag().equals(imagePath)) {
            ImageUtils.loadImageWithLock(getContext(),holder.image,R.mipmap.def_portrait);
           /* ImageLoader.getInstance().loadImage(imagePath, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String imageUri, View view) {

                }

                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                }

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    holder.image.setBackgroundDrawable(new BitmapDrawable(loadedImage));
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {

                }
            });*/
        }

        return convertView;
    }

    class ViewHolder {
        public ImageView image;
    }
}
